package org.example.account.controller;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.example.order.domain.Order;
import org.example.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import org.example.account.domain.Account;
import org.example.account.service.IAccountService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 对账单Controller
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-20
 */
@Controller
@RequestMapping("/account/account")
public class AccountController extends BaseController
{
    private String prefix = "account/account";

    @Autowired
    private IAccountService accountService;

    @GetMapping("/partnerAccount")
    public String account()
    {
        return prefix + "/partnerAccount";
    }
    @GetMapping()
    public String account(Integer type,String contractId,String supplierId,ModelMap mmp)
    {
        mmp.put("type",type);
        mmp.put("contractId",contractId);
        mmp.put("supplierId",supplierId);
        return prefix + "/account";
    }

    /**
     * 查询对账单列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Account account)
    {
        startPage();
        List<Account> list = accountService.selectAccountList(account);
        return getDataTable(list);
    }

    /**
     * 导出对账单列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Account account)
    {
        List<Account> list = accountService.selectAccountList(account);
        ExcelUtil<Account> util = new ExcelUtil<Account>(Account.class);
        return util.exportExcel(list, "对账单数据");
    }

    /**
     * 新增对账单
     */
    @GetMapping("/add")
    public String add(String contractId,ModelMap mmp)
    {
        mmp.put("contractId",contractId);
        return prefix + "/add";
    }

    /**
     * 新增保存对账单
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Account account)
    {
        account.setAccountNo(getCurrentAccountNo(account));
        return toAjax(accountService.insertAccount(account));
    }

    private String getCurrentAccountNo(Account account) {
        account.setCreateTime(DateUtils.getNowDate());
        startPage();
        List<Account> list = accountService.selectAccountList(account);
        TableDataInfo dataTable = getDataTable(list);
        Long total =dataTable.getTotal()+1L;
        String s2 = String.format("%04d", total);
        String accountNo = null;
        if(Util.CONTRACT_TYPE_SELL.equals(account.getType().toString())){
            accountNo =  "AR"+DateUtils.dateTimeNow("yyMMdd")+s2;
        }else{
            accountNo =  "AP"+DateUtils.dateTimeNow("yyMMdd")+s2;
        }
        return accountNo;
    }
    /**
     * 修改对账单
     */
    @GetMapping("/edit/{accountId}")
    public String edit(@PathVariable("accountId") Long accountId, ModelMap mmap)
    {
        Account account = accountService.selectAccountByAccountId(accountId);
        mmap.put("account", account);
        return prefix + "/edit";
    }

    /**
     * 修改保存对账单
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Account account)
    {
        return toAjax(accountService.updateAccount(account));
    }

    /**
     * 删除对账单
     */
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(accountService.deleteAccountByAccountIds(ids));
    }
}
